package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysFlowMarketHistoryMapper;
import com.settlement.system.mapper.SysFlowMarketMapper;
import com.settlement.system.model.entity.SysFlowMarket;
import com.settlement.system.model.entity.SysFlowMarketHistory;
import com.settlement.system.model.form.SysFlowMarketHistoryForm;
import com.settlement.system.model.query.SysFlowMarketHistoryQuery;
import com.settlement.system.model.vo.SysFlowMarketHistoryImportVo;
import com.settlement.system.service.SysFlowMarketHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 流程市场历史版本表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-01-08
 */
@Service
@RequiredArgsConstructor
public class SysFlowMarketHistoryServiceImpl extends ServiceImpl<SysFlowMarketHistoryMapper, SysFlowMarketHistory> implements SysFlowMarketHistoryService {

    private final SysFlowMarketMapper sysFlowMarketMapper;

    @Override
    public Page<SysFlowMarketHistory> getPage(SysFlowMarketHistoryQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysFlowMarketHistory> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysFlowMarketHistory(SysFlowMarketHistoryForm sysFlowMarketHistoryForm) {
        SysFlowMarketHistory sysFlowMarketHistory = new SysFlowMarketHistory();
        BeanUtil.copyProperties(sysFlowMarketHistoryForm, sysFlowMarketHistory);
        sysFlowMarketHistory.setCreateTime(null);
        return this.saveOrUpdate(sysFlowMarketHistory);
    }

    @Override
    public SysFlowMarketHistoryForm getSysFlowMarketHistoryFormData(Long id) {
        SysFlowMarketHistory sysFlowMarketHistory = this.getById(id);
        SysFlowMarketHistoryForm sysFlowMarketHistoryForm = new SysFlowMarketHistoryForm();
        BeanUtil.copyProperties(sysFlowMarketHistory, sysFlowMarketHistoryForm);
        return sysFlowMarketHistoryForm;
    }

    @Override
    public boolean deleteSysFlowMarketHistory(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysFlowMarketHistoryImportVo> voList) {
        List<SysFlowMarketHistory> sysFlowMarketHistoryList = new ArrayList<>();
        for (SysFlowMarketHistoryImportVo importVo : voList) {
            SysFlowMarketHistory sysFlowMarketHistory = new SysFlowMarketHistory();
            BeanUtil.copyProperties(importVo, sysFlowMarketHistory);
            sysFlowMarketHistoryList.add(sysFlowMarketHistory);
        }
        this.saveOrUpdateBatch(sysFlowMarketHistoryList);
    }

    @Override
    public List<SysFlowMarketHistoryImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysFlowMarketHistory> sysFlowMarketHistoryList = this.getBaseMapper().selectBatchIds(ids);
        List<SysFlowMarketHistoryImportVo> voList = new ArrayList<>();
        for (SysFlowMarketHistory sysFlowMarketHistory : sysFlowMarketHistoryList) {
            SysFlowMarketHistoryImportVo vo = new SysFlowMarketHistoryImportVo();
            BeanUtil.copyProperties(sysFlowMarketHistory, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<SysFlowMarketHistory> getHistoryList(String flowId) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<SysFlowMarketHistory>().eq(SysFlowMarketHistory::getFlowId, flowId).orderByDesc(SysFlowMarketHistory::getCreateTime));
    }

    @Override
    public boolean rollBack(String flowId, String historyId) {
        SysFlowMarket sysFlowMarket = sysFlowMarketMapper.selectById(flowId);
        SysFlowMarketHistory sysFlowMarketHistory = this.baseMapper.selectById(historyId);
        // 回滚到历史版本
        sysFlowMarket.setFilePath(sysFlowMarketHistory.getFilePath());
        // 文件大小
        sysFlowMarket.setFileSize(sysFlowMarketHistory.getFileSize());
        // 保存
        sysFlowMarketMapper.updateById(sysFlowMarket);
        return true;
    }
}
